package com.xframe.domain.views.builder;

import cn.hutool.json.JSONUtil;
import com.xframe.domain.emnus.Constant;
import com.xframe.domain.emnus.PublicFieldEnum;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysModel;
import com.xframe.domain.sys.SysModelAttr;
import com.xframe.domain.views.ui.FilterEnum;
import com.xframe.domain.views.ui.TableUIBill;
import com.xframe.domain.views.ui.TableUIModel;
import com.xframe.domain.views.ui.UIType;
import com.xframe.xdal.core.annotation.XGridTableRef;
import com.xframe.xdal.core.annotation.XQueryCondition;
import com.xframe.xdal.core.model.mapping.IColumnMapping;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.util.StringUtil;
import com.xframe.xdal.core.util.XReflect;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 生成实体的网格配置
 * @ClassName: TableUIModelBuilder
 * @Author: xufeng
 * @CreateTime: 2023/12/06
 **/
@Log4j2
public class TableUIModelBuilder extends UIModelBuilder  {

    static final String VIEW_NAME = "defaultTableView";
    static final String CONFIG_TYPE = "tableView";
    static final String RENDER_CLASS = "xTableView";


    @Override
    public String getViewName() {
        return VIEW_NAME;
    }

    @Override
    public String getConfigType() {
        return CONFIG_TYPE;
    }

    @Override
    public String getRenderClass() {
        return RENDER_CLASS;
    }

    @Override
    public String setSysModelViewConfigContent(SysModel sysModel, IUserCredentials userCredentials) {
        List<SysModelAttr> sysModelAttrList = sysModel.getModelAttrList();
        List<TableUIModel> tableUIModelList = new ArrayList<>();
        int indexNo = 0;
        for(SysModelAttr modelAttr : sysModelAttrList){
            indexNo = indexNo + 10;
            TableUIModel tableUIModel = new TableUIModel();
            tableUIModel.setAttrName(modelAttr.getAttrName());
            tableUIModel.setLabel(TableUIModel.NAME_SIGE + modelAttr.getAttrName());
            tableUIModel.setRemark(modelAttr.getDesc());
            tableUIModel.setComponentName(UIType.LABEL.getComponentName());
            tableUIModel.setSortNo(indexNo);
            tableUIModel.setProp(modelAttr.getAttrName());
            tableUIModel.setShow(true);
            tableUIModel.setSortFlag(true);
            if(modelAttr.getPrimaryKeyFlag() || modelAttr.getAttrName().equals(PublicFieldEnum.VERSION.getValue())){
                tableUIModel.setComponentName(UIType.NONE.getComponentName());
                tableUIModel.setShow(false);
                tableUIModel.setSortFlag(false);
            }
            // 判断是不是引用字段
            IRelation relation = findRefAttr(modelAttr,sysModel);
            if(relation != null){
                tableUIModel.setComponentName(UIType.NONE.getComponentName());
                tableUIModel.setShow(false);
                tableUIModel.setSortFlag(false);
                indexNo = builderRefTableUIModel(relation,tableUIModelList,indexNo);
            }
            if(modelAttr.getAttrName().equals(PublicFieldEnum.DEL_FLAG.getValue())){
                tableUIModel.setFilter(FilterEnum.STATUS_FILTER.getName());
                tableUIModel.setFilterArgs(StringUtil.EMPTY);
            }
            tableUIModelList.add(tableUIModel);
        }
        TableUIBill tableUIBill = new TableUIBill();
        tableUIBill.setElements(tableUIModelList);
        tableUIBill.setConfigType(getConfigType());
        return JSONUtil.toJsonStr(tableUIBill);
    }




    private int builderRefTableUIModel(IRelation relation,List<TableUIModel> tableUIModelList,int indexNo){
        //relation.getDataType()
        SysModel sysModel = SysModel.getInstance(relation.getDataType().getName());
        Map<String, XGridTableRef> map = XReflect.getFieldAnnotationMap(relation.getDataType(), XGridTableRef.class);
        Integer indexSortNo = indexNo;
        if(sysModel != null) {
            // 找出所有的非公有字段
            List<SysModelAttr> sysModelAttrList = sysModel.getModelAttrList();
            for (SysModelAttr modelAttr : sysModelAttrList) {
                if(map.containsKey(modelAttr.getAttrName())) {
                    XGridTableRef gridTableRef = map.get(modelAttr.getAttrName());
                    indexSortNo = indexSortNo + 10;
                    TableUIModel tableUIModel = new TableUIModel();
                    tableUIModel.setAttrName(relation.getAs() + Constant.POINT_SYMBOL + modelAttr.getAttrName());
                    tableUIModel.setLabel(TableUIModel.NAME_SIGE + relation.getAs() + Constant.POINT_SYMBOL + modelAttr.getAttrName());
                    tableUIModel.setRemark(modelAttr.getDesc());
                    tableUIModel.setComponentName(UIType.LABEL.getComponentName());
                    tableUIModel.setSortNo(indexSortNo);
                    tableUIModel.setProp(relation.getAs() + Constant.POINT_SYMBOL + modelAttr.getAttrName());
                    tableUIModel.setShow(true);
                    tableUIModel.setSortFlag(false);
                    if (modelAttr.getPrimaryKeyFlag() || modelAttr.getAttrName().equals(PublicFieldEnum.VERSION.getValue())) {
                        tableUIModel.setComponentName(UIType.NONE.getComponentName());
                        tableUIModel.setShow(false);
                        tableUIModel.setSortFlag(false);
                    }
                    if(modelAttr.getAttrName().equals(PublicFieldEnum.DEL_FLAG.getValue())){
                        tableUIModel.setWidth("110");
                    }
                    if(modelAttr.getAttrName().equals(PublicFieldEnum.CREATE_BY.getValue()) ||
                            modelAttr.getAttrName().equals(PublicFieldEnum.UPDATE_BY.getValue())){
                        tableUIModel.setWidth("100");
                    }
                    if(modelAttr.getAttrName().equals(PublicFieldEnum.CREATE_TIME.getValue()) ||
                            modelAttr.getAttrName().equals(PublicFieldEnum.CREATE_TIME.getValue())){
                        tableUIModel.setWidth("140");
                    }
                    if(StringUtil.isNotEmpty(gridTableRef.filter())) {
                        tableUIModel.setFilter(gridTableRef.filter());
                    }
                    tableUIModelList.add(tableUIModel);
                }
            }
        }else {
            ITableInfo tableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(relation.getDataType());

            for (IColumnMapping modelAttr : tableInfo.getColumnMappingList()) {
                if(map.containsKey(modelAttr.getFieldName())) {
                    XGridTableRef gridTableRef = map.get(modelAttr.getFieldName());
                    indexSortNo = indexSortNo + 10;
                    TableUIModel tableUIModel = new TableUIModel();
                    tableUIModel.setAttrName(relation.getAs() + Constant.POINT_SYMBOL + modelAttr.getFieldName());
                    tableUIModel.setLabel(TableUIModel.NAME_SIGE + relation.getAs() + Constant.POINT_SYMBOL + modelAttr.getFieldName());
                    tableUIModel.setRemark(modelAttr.getDesc());
                    tableUIModel.setComponentName(UIType.LABEL.getComponentName());
                    tableUIModel.setSortNo(indexSortNo);
                    tableUIModel.setProp(relation.getAs() + Constant.POINT_SYMBOL + modelAttr.getFieldName());
                    tableUIModel.setShow(true);
                    tableUIModel.setSortFlag(false);
                    if (modelAttr.getPrimaryKeyFlag() || modelAttr.getFieldName().equals(PublicFieldEnum.VERSION.getValue())) {
                        tableUIModel.setComponentName(UIType.NONE.getComponentName());
                        tableUIModel.setShow(false);
                        tableUIModel.setSortFlag(false);
                    }
                    if(StringUtil.isNotEmpty(gridTableRef.filter())) {
                        tableUIModel.setFilter(gridTableRef.filter());
                    }
                    if(modelAttr.getFieldName().equals(PublicFieldEnum.DEL_FLAG.getValue())){
                        tableUIModel.setWidth("110");
                    }
                    if(modelAttr.getFieldName().equals(PublicFieldEnum.CREATE_BY.getValue()) ||
                            modelAttr.getFieldName().equals(PublicFieldEnum.UPDATE_BY.getValue())){
                        tableUIModel.setWidth("110");
                    }
                    if(modelAttr.getFieldName().equals(PublicFieldEnum.CREATE_TIME.getValue()) ||
                            modelAttr.getFieldName().equals(PublicFieldEnum.CREATE_TIME.getValue())){
                        tableUIModel.setWidth("160");
                    }
                    tableUIModelList.add(tableUIModel);
                }
            }
        }
        return indexSortNo;
    }


}
